/*
 * Copyright (C) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef LOTTIETURBO_LOTTIE_XCOMPONENT_NODE_H
#define LOTTIETURBO_LOTTIE_XCOMPONENT_NODE_H

#include "controller/lottie_handler.h"
#include "render/lottie_render.h"
#include <ace/xcomponent/native_interface_xcomponent.h>
#include <arkui/native_node.h>
#include <arkui/native_type.h>
#include <cstdint>
#include <string>

namespace LottieTurbo {

class LottieXcomponentNode {
public:
    struct CalculatePosition {
        bool keepAspectRatio{true};
        int width{0};
        int height{0};
        int x{0};
        int y{0};
    };

public:
    explicit LottieXcomponentNode(std::string xcomponentId);
    ~LottieXcomponentNode();
    ArkUI_NodeHandle GetHandle() const;

    /**
     * @brief 创建Xcomponent组件并节点上树
     * @param xcomponentId 组件Id
     */
    void CreateXcomponentNode(std::string xcomponentId);

    /**
     * 初始化回调函数并渲染第一帧
     */
    void Init();

    /**
     * 调度渲染任务到线程池
     * @param absoluteFrameToPlay 要渲染的绝对帧
     * @param current 当前索引
     */
    void ScheduleRenderTask(float absoluteFrameToPlay, int64_t current);

    /**
     * 渲染当前帧（仅绘制）
     */
    void RenderCurrentFrame();

    /**
     * 渲染当前帧并推进到下一个待绘制帧
     */
    void RenderAndAdvanceFrame();

    /**
     * 更新布局模式
     */
    void OnUpdateContentMode();

    /**
     * 设置定时器
     */
    void SetAnimator();

    /**
     * 绑定LottieHandler
     */
    void SetHandler(std::shared_ptr<LottieHandler> LottieHandler);

private:
    static void OnSurfaceCreated(OH_NativeXComponent *component, void *window);
    static void OnDispatchTouchEvent(OH_NativeXComponent *component, void *window);
    static void OnSurfaceChanged(OH_NativeXComponent *component, void *window);
    static void OnSurfaceDestroyed(OH_NativeXComponent *component, void *window);
    /**
     * 渲染首帧和重置lottie宽高刷新渲染:
     * OnSurfaceCreated回调时渲染首帧
     * OnSurfaceChanged回调时重置lottie宽高刷新渲染
     * @param component XComponent回调的OH_NativeXComponent指针
     * @param window XComponent回调的window指针
     * @param isOnSurfaceCreated 用于区分OnSurfaceCreated和OnSurfaceChanged的回调
     * @return void
     */
    static void RenderOnSurfaceChanged(OH_NativeXComponent *component, void *window, bool isOnSurfaceCreated);
    OH_NativeXComponent *nativeXComponent_{nullptr};
    ArkUI_NativeNodeAPI_1 *arkUINativeNodeApi_{nullptr};
    ArkUI_NodeHandle handle_;
    std::shared_ptr<LottieHandler> lottieHandler_{nullptr};
    std::shared_ptr<LottieRender> lottieRender_{nullptr};
    std::string contentMode_;
    CalculatePosition position_;
    std::atomic<int8_t> status_{0};
    bool firstFrame_{true};
};
} // namespace LottieTurbo
#endif // LOTTIETURBO_LOTTIE_XCOMPONENT_NODE_H
